// 该文件代码已作废
import { queryVideoList } from "@/api/news";
import dayjs from "dayjs";
import { insertAdvert } from "@/utils";
function getYouTubeVideoId(url: string): string | null {
  const regex =
    /(?:https?:\/\/)?(?:www\.)?(?:youtube\.com\/(?:[^\/\n\s]+\/\S+\/|(?:v|e(?:mbed)?)\/|\S*?[?&]v=)|youtu\.be\/)([a-zA-Z0-9_-]{11})/;
  const match = url.match(regex);
  return match ? match[1] : null;
}

export const useFetchVid = defineStore("cached-video", () => {
  let page = 1;
  const cachedVideoList = ref<any[]>([]);
  const tabVideoList = ref<any[]>([]);
  const loadVidState = ref<"loadmore" | "loading" | "nomore">("loadmore");
  function fetchVideos() {
    return new Promise((resolve, reject) => {
      if (loadVidState.value === "nomore") return reject();
      loadVidState.value = "loading";
      queryVideoList(page++)
        .then((res: any) => {
          const data = (res || []).map((item: any) => {
            return {
              id: item.id,
              src: item.image,
              title: item.title,
              time: item.addtime
                ? dayjs(+String(item.addtime).padEnd(13, "0")).format(
                    "YYYY-MM-DD HH:mm:ss"
                  )
                : "",
              author: item.author,
              link: item.link,
              type: "video",
              imgWidth: "100%",
              imgHeight: "auto",
              // 视频页面使用
              video_url: item.link,
              youtubeId: getYouTubeVideoId(item.link) || "",
              video_cover: item.image,
              add_username: item.author,
              content: item.title
            };
          });
          if (data.length < 6) {
            loadVidState.value = "nomore";
          } else {
            loadVidState.value = "loadmore";
          }

          const lastAdvertIndex = unref(cachedVideoList).findLastIndex(
            (item: any) => item.type === "advert"
          );
          let lastEles = [];
          let preEles = [];
          if (lastAdvertIndex !== -1) {
            lastEles = unref(cachedVideoList).slice(lastAdvertIndex + 1);
            preEles = unref(cachedVideoList).slice(0, lastAdvertIndex + 1);
          } else {
            preEles = [];
            lastEles = [...unref(cachedVideoList)];
          }
          const currData = insertAdvert(
            [...lastEles, ...data],
            { type: "advert", source: "news" },
            5
          );
          tabVideoList.value = preEles.concat(currData);
          cachedVideoList.value = cachedVideoList.value.concat(data);
          resolve(cachedVideoList);
        })
        .catch(() => {
          reject();
          loadVidState.value = "loadmore";
        });
    });
  }
  function setCachedVideos(videos: any[]) {
    cachedVideoList.value = videos;
  }
  function resetCachedVideos() {
    cachedVideoList.value = [] as string[];
  }
  return {
    cachedVideoList,
    fetchVideos,
    setCachedVideos,
    resetCachedVideos,
    tabVideoList,
    loadVidState
  };
});

export const useFetchVidRefs = () => storeToRefs(useFetchVid());
